package com.scau.clothes.api.stock;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;
import com.scau.clothes.core.core.entity.AjaxResult;
import com.scau.clothes.stock.model.dto.ClothesDto;
import com.scau.clothes.stock.model.entity.Clothes;
import com.scau.clothes.stock.model.entity.Color;
import com.scau.clothes.stock.service.ClothesService;
import com.scau.clothes.stock.service.ColorService;
import io.swagger.annotations.ApiOperation;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;
@RequestMapping("/color")
@RestController
public class ColorController {
    @Resource
    private ColorService colorService;

    @PostMapping("/listAllColor")
    @ApiOperation("查询颜色")
    public AjaxResult listAllColor(@RequestBody Color color){
        List<Color> list = colorService.listAllColor(color);
        return AjaxResult.success("查询成功",list);
    }

    @PostMapping("/insertColor")
    @ApiOperation("新增颜色")
    public AjaxResult insertColor(@RequestBody Color color){
        int rows = colorService.insertColor(color);
        return rows > 0 ? AjaxResult.success("添加颜色成功") : AjaxResult.error("添加颜色失败");
    }

    @PostMapping("/updateColor")
    @ApiOperation("修改颜色信息")
    public AjaxResult updateColor(@RequestBody Color color){
        int rows = colorService.updateColor(color);
        return rows > 0 ? AjaxResult.success("修改颜色成功") : AjaxResult.error("修改颜色失败");
    }

    @PostMapping("/deleteColor")
    @ApiOperation("删除颜色")
    public AjaxResult deleteColor(@RequestBody Color color){
        int rows = colorService.deleteColor(color);
        return rows > 0 ? AjaxResult.success("删除颜色成功") : AjaxResult.error("删除颜色失败");
    }

    @GetMapping("/page")
    @ApiOperation("分页操作")
    public AjaxResult page(@RequestParam("pageNum") Integer pageNum, @RequestParam("pageSize") Integer pageSize, Color color){
        List<Color> list = colorService.listAllColor(color);
        Page page = new Page(pageNum, pageSize);
        int total = list.size();
        page.setTotal(total);
        int startIndex = (pageNum - 1) * pageSize;
        int endIndex = Math.min(startIndex + pageSize,total);
        page.addAll(list.subList(startIndex,endIndex));
        PageInfo pageInfo = new PageInfo<>(page);
        return AjaxResult.success(pageInfo);
    }
}
